<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 50px;
        }

        .title {
            text-align: center;
        }

        .box .block {
            margin: 30px 0;
        }

        .box .block input {
            width: 100%;
            height: 40px;
            padding-left: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            outline: none;
            box-sizing: border-box;
        }

        .button {
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="title">注册新账户</div>
        <div class="block">
            <input type="text" placeholder="请输入帐号">
        </div>
        <div class="block">
            <input type="password" placeholder="请输入密码">
        </div>
        <div class="block">
            <input type="password" placeholder="确认密码">
        </div>

        <div class="block">
            <input type="button" value="注册" class="button">
        </div>
    </div>
    <script src="./ajax.js"></script>
</body>

</html>
<SCript>
    //获取所有input输入
    var inputs = document.querySelectorAll('.box .block input')
    //给button绑定点击事件
    inputs[3].onclick = function () {
        //对表单内容做校验（为空校验，格式校验）
        if (inputs[0].value && inputs[1].value) {
            if (/^1[3-9]\d{9}$/.test(inputs[0].value) && /^\d{4}$/.test(inputs[1].value)) {
                if (/^\d{4}$/.test(inputs[1].value) == /^\d{4}$/.test(inputs[2].value)) {
                    //通过校验, 发起登陆请求
                    ajax('get', 'https://yantianfeng.com/api/register', {
                        username: inputs[0].value,
                        password: inputs[1].value,
                        password: inputs[2].value,
                    }, function (data) {
                        console.log(data);
                    })
                } else {
                    alert('请输入正确的手机号/密码')
                }
            } else {
                alert('用户名/密码不能为空!')
            }
        }
    }
</SCript>